package com.cloud.mall.pms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.pms.dto.PmsBrandParam;
import com.cloud.mall.pms.mapper.PmsBrandMapper;
import com.cloud.mall.pms.mapper.PmsProductMapper;
import com.cloud.mall.pms.model.PmsBrand;
import com.cloud.mall.pms.model.PmsBrandExample;
import com.cloud.mall.pms.model.PmsProduct;
import com.cloud.mall.pms.model.PmsProductExample;
import com.cloud.mall.pms.out.PmsBrandOut;
import com.cloud.mall.pms.service.PmsBrandService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PmsBrandServiceImpl implements PmsBrandService {
    @Autowired
    private PmsBrandMapper brandMapper;
    @Autowired
    private PmsProductMapper productMapper;

    @Transactional
    @Override
    public BaseResponse create(@RequestBody PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        return BaseResponse.setResultSuccess(brandMapper.insertSelective(pmsBrand));
    }

    @Override
    public BaseResponse delete(Long id) {
        return BaseResponse.setResultSuccess(brandMapper.deleteByPrimaryKey(id));
    }

    @Transactional
    @Override
    public BaseResponse update(Long id, PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        pmsBrand.setId(id);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        //更新品牌时要更新商品中的品牌名称
        PmsProduct product = new PmsProduct();
        product.setBrandName(pmsBrand.getName());
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andBrandIdEqualTo(id);
        productMapper.updateByExampleSelective(product,example);
        int count=brandMapper.updateByPrimaryKeySelective(pmsBrand);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse<List<PmsBrandOut>> getList() {
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(brandMapper.selectByExample(new PmsBrandExample()),PmsBrandOut.class));
    }

    @Override
    public BaseResponse<CommonPage<PmsBrandOut>> getList(String keyword, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.setOrderByClause("sort desc");
        PmsBrandExample.Criteria criteria = pmsBrandExample.createCriteria();
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andNameLike("%" + keyword + "%");
        }
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(brandMapper.selectByExample(pmsBrandExample),PmsBrandOut.class)));
    }

    @Override
    public BaseResponse<PmsBrandOut> getItem(Long id) {
        PmsBrand pmsBrand = brandMapper.selectByPrimaryKey(id);
        return BaseResponse.setResultSuccess(MallBeanUtils.dtoToDo(pmsBrand,PmsBrandOut.class));
    }

    @Override
    public BaseResponse deleteBatch(List<Long> ids) {
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        int count= brandMapper.deleteByExample(pmsBrandExample);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse updateShowStatus(List<Long> ids, Integer showStatus) {
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setShowStatus(showStatus);
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        int count=brandMapper.updateByExampleSelective(pmsBrand, pmsBrandExample);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setFactoryStatus(factoryStatus);
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        int count=brandMapper.updateByExampleSelective(pmsBrand, pmsBrandExample);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }


}
